//
//  InformationScrollView.m
//  Vlink
//
//  Created by 赵隆杰 on 2021/10/9.
//  Copyright © 2021 myTV. All rights reserved.
//

#import "InformationScrollView.h"
#import "InformationTopView.h"

@interface InformationScrollView () <UIScrollViewDelegate>

@property (nonatomic, assign) BOOL scrollCanScroll;

@end

@implementation InformationScrollView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.backgroundColor = [UIColor whiteColor];
        self.delegate = self;
        self.scrollCanScroll = YES;
        self.showsVerticalScrollIndicator = NO;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollNotification:) name:@"Information_scrollCanScroll" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollHeaderRefreshStop:) name:@"Information_scrollHeaderRefreshStop" object:nil];
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.mj_header beginRefreshing];
//            NSLog(@"-*- 通知 table 上拉刷新");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Information_tableHeaderRefresh" object:nil userInfo:nil];
            
        }];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Information_scrollCanScroll" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Information_scrollHeaderRefreshStop" object:nil];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"scroll -- %.2f", scrollView.contentOffset.y);
    
    CGFloat maxOffsetY = [InformationTopView height];
    if (scrollView.contentOffset.y >= maxOffsetY) {
        [scrollView setContentOffset:CGPointMake(0, maxOffsetY)];
        _scrollCanScroll = NO;

//        NSLog(@"-*- 通知 table 可滑动");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Information_tableCanScroll" object:nil userInfo:@{@"canScroll":@"1"}];
    } else {
        if (!_scrollCanScroll) {
            [scrollView setContentOffset:CGPointMake(0, maxOffsetY)];
        }
    }
}

#pragma mark - NSNotificationCenter
- (void)scrollNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if (canScroll.boolValue) {
        _scrollCanScroll = YES;
    }
}
- (void)scrollHeaderRefreshStop:(NSNotification *)notification
{
    [self.mj_header endRefreshing];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}


@end
